package com.ax.api.route.associator.shopassistant;

import com.ax.api.route.util.RouteConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @Author Lg
 * @Date 2018/9/4 11:04
 * @Content
 */
@RestController
@RequestMapping("/web/basePersonnel/")
public class BasePersonnelController {



    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;

    /**
     * 新增店员综合信息
     * @param imageFile
     * @return
     */
    @RequestMapping("insertInformation")
    public ResponseEntity<String> upload(@RequestParam Map<String,Object> map){
        String url = geturl("/basePersonnel/insertInformation", RouteConfig.ASSOCIATOR_ROUTE);
        return restTemplate.postForEntity(url,map,String.class);
    }

    /**
     * 查询店员
     * @param imageFile
     * @return
     */
    @RequestMapping("queryPersonnel")
    public ResponseEntity<String> queryPersonnel(@RequestParam Map<String,Object> map){
        String url = geturl("/basePersonnel/queryPersonnel", RouteConfig.ASSOCIATOR_ROUTE);
        return restTemplate.postForEntity(url,map,String.class);
    }

    /**
     * 修改店员综合信息
     * @param imageFile
     * @return
     */
    @RequestMapping("updateFlag")
    public ResponseEntity<String> updateFlag(@RequestParam Map<String,Object> map){
        String url = geturl("/basePersonnel/updateFlag", RouteConfig.ASSOCIATOR_ROUTE);
        return restTemplate.postForEntity(url,map,String.class);
    }

    /**
     * 删除店员综合信息
     * @return
     */
    @RequestMapping("deleteBasePersonnel")
    public ResponseEntity<String> upload(@RequestParam Long personnelId){
        String url = geturl("/basePersonnel/deleteBasePersonnel", RouteConfig.ASSOCIATOR_ROUTE);
        return restTemplate.postForEntity(url,personnelId,String.class);
    }

    /**
     * 查询员工姓名,id,电话
     * @return
     */
    @RequestMapping("queryDto")
    public ResponseEntity<String> queryDto(){
        String url = geturl("/basePersonnel/queryDto", RouteConfig.ASSOCIATOR_ROUTE);
        return restTemplate.postForEntity(url,null,String.class);
    }
    /**
     * 查询菜单
     * @return
     */
    @RequestMapping("queryMenuDto")
    public ResponseEntity<String> queryMenuDto(){
        String url = geturl("/basePersonnel/queryMenuDto", RouteConfig.ASSOCIATOR_ROUTE);
        return restTemplate.postForEntity(url,null,String.class);
    }

    /**
     * 新增用户菜单
     * @param ajaxRes
     * @param map
     * @return
     */
    @RequestMapping("insertPersonnelMenu")
    public ResponseEntity<String> insertPersonnelMenu(@RequestParam Map<String,Object> map){
        //**asd*asjgh***
        String url = geturl("/basePersonnel/insertPersonnelMenu", RouteConfig.ASSOCIATOR_ROUTE);
        url.trim();
        return restTemplate.postForEntity(url,map,String.class);
    }


    /**
     * 查询用户角色菜单
     * @param map
     * @return
     */
    @RequestMapping("queryMenuRole")
    public ResponseEntity<String> queryMenuRole(@RequestParam Map<String,Object> map){
        //**asd*asjgh***
        String url = geturl("/basePersonnel/queryMenuRole", RouteConfig.ASSOCIATOR_ROUTE);
        url.trim();
        return restTemplate.postForEntity(url,map,String.class);
    }



    public String geturl(String path, String config){
        ServiceInstance serviceInstance = this.loadBalancerClient.choose(config);
        //	 String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort()+path;
        StringBuffer bf = new StringBuffer("http://");
        bf.append(serviceInstance.getHost());
        bf.append(":");
        bf.append(serviceInstance.getPort());
        bf.append(path);
        return bf.toString();
    }







}
